<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>{{weibo.title}}</h1>
    <p>{{weibo.content}}</p>
    <p>{{weibo.author}}发布于{{weibo.created}}</p>
    <img :src="weibo.url" style="width: 300px" alt="">
    <hr>
    <h2>微博评论</h2>
    <div v-for="c in arr">
        <h4>{{c.content}}</h4>
        <p>{{c.author}}发布于{{c.created}}</p>
    </div>
    <div></div>
    <form action="">
        <input type="text" name="content" placeholder="说点什么呢...">
        <input type="button" value="发布评论" @click="comment()">
    </form>
</div>
<!--引入CDN服务器的框架文件-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            weibo: {},
            arr:[]
        },
        created: function () {
            let id = location.search.split("=")[1];
            axios.get("/weibo/selectById?id=" + id).then(function (response) {
                v.weibo = response.data;
            })
            axios.get("/comment/selectByWeiboId?id="+id).then(function (response) {
                v.arr= response.data;
            })
        },
        methods: {
            comment: function () {
                let data = new FormData(document.querySelector("form"));
                //把当前微博的id传递过去
                let id = location.search.split("=")[1]
                data.set("weiboId",id);

                axios.post("/comment/insert", data).then(function (response) {
                    if (response.data == 1) {
                        alert("评论完成")
                        location.reload();
                    } else {
                        alert("请先登录222");
                        location.href = "/login.html"
                    }

                })
            }
        }
    })
</script>
</body>
</html>